using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using C1.C1Schedule;
using C1.Xaml.Schedule;
using C1.Xaml;
using Windows.UI;
// User Control 項目テンプレートは、http://go.microsoft.com/fwlink/?LinkId=234236 で文書化されています。
namespace SchedulerTest3
{
public sealed partial class EditAppointmentControl : UserControl
{
#region ** fields
private C1FullscreenDialog _parentWindow = null;
private Appointment _appointment;
private C1Scheduler _scheduler;
private bool _isLoaded = false;
private TimeSpan _defaultStart;
private TimeSpan _defaultDuration;
private bool _updatingRecurrence = false;
#endregion
//-----------------------------------------------------------------------------------
#region ** initialization
/// <summary>
/// <see cref="EditAppointmentControl"/> クラスの新しいインスタンスを作成します。
/// </summary>
public EditAppointmentControl()
{
this.InitializeComponent();
// 繰り返しコンボを塗りつぶします
List<string> recTypes = new List<string>();
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recOnce);
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recDaily);
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recWeekdays);
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recMonday);
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recTuesday);
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recWeekly);
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recMonthly);
recTypes.Add(C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.recYearly);
howOften.ItemsSource = recTypes;
}
private void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
if (DataContext != null)
{
_appointment = DataContext as Appointment;
_defaultStart = _appointment.AllDayEvent ? TimeSpan.FromHours(8) : _appointment.Start.TimeOfDay;
_defaultDuration = _appointment.AllDayEvent ? TimeSpan.FromMinutes(30) : _appointment.Duration;
if (_appointment != null)
{
if (_appointment.ParentCollection != null)
{
_scheduler = _appointment.ParentCollection.ParentStorage.ScheduleStorage.Scheduler;
if (_appointment.AllDayEvent)
{
_defaultStart = _scheduler.CalendarHelper.StartDayTime;
_defaultDuration = _scheduler.CalendarHelper.Info.TimeScale;
}
}
UpdateWindowHeader();
UpdateRecurrenceState();
reminderTime.Value = _appointment.ReminderTimeBeforeStart;
UpdateCalendars();
if (_appointment.AllDayEvent)
{
startCalendar.EditMode = endCalendar.EditMode = C1.Xaml.DateTimeEditors.C1DateTimePickerEditMode.Date;
}
else
{
startCalendar.EditMode = endCalendar.EditMode = C1.Xaml.DateTimeEditors.C1DateTimePickerEditMode.DateTime;
}
}
}
}
private void EditAppointmentControl_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (!_isLoaded)
{
_parentWindow = (C1FullscreenDialog)VTreeHelper.GetParentOfType(this, typeof(C1FullscreenDialog));
if (_parentWindow != null && _appointment != null)
{
_parentWindow.Unloaded += _parentWindow_Unloaded;
_isLoaded = true;
}
}
subject.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
void _parentWindow_Unloaded(object sender, RoutedEventArgs e)
{
_parentWindow.Unloaded -= _parentWindow_Unloaded;
DataContext = null;
_scheduler = null;
_appointment = null;
_parentWindow = null;
}
#endregion
//-----------------------------------------------------------------------------------
#region ** object model
/// <summary>
/// 現在の DataContext を表す <see cref="Appointment"/> オブジェクトを取得または設定します。
/// </summary>
public Appointment Appointment
{
get
{
return _appointment;
}
set
{
_appointment = value;
if (_parentWindow != null)
{
_parentWindow.Content =
_parentWindow.DataContext = value;
}
DataContext = value;
if (_appointment != null)
{
UpdateWindowHeader();
UpdateRecurrenceState();
}
}
}
/// <summary>
/// 予定ウィンドウのヘッダーとして使用される <see cref="String"/> 値を取得します。
/// </summary>
public string Header
{
get { return (string)GetValue(HeaderProperty); }
private set { SetValue(HeaderProperty, value); }
}
/// <summary>
/// <see cref="Header"/> 依存プロパティを識別します。
/// </summary>
private static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(string),
typeof(EditAppointmentControl), new PropertyMetadata(string.Empty));
#endregion
//-----------------------------------------------------------------------------------
#region ** recurrence properties
private void UpdateRecurrenceState()
{
if (_appointment == null || _updatingRecurrence)
{
return;
}
if (_appointment.RecurrenceState == RecurrenceStateEnum.NotRecurring)
{
recPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
howOften.IsEnabled = true;
howOften.SelectedIndex = 0;
}
else
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
switch (pattern.RecurrenceType)
{
case RecurrenceTypeEnum.Daily:
howOften.SelectedIndex = 1;
break;
case RecurrenceTypeEnum.Workdays:
howOften.SelectedIndex = 2;
break;
case RecurrenceTypeEnum.Weekly:
WeekDaysEnum mask = pattern.DayOfWeekMask;
if ((mask & WeekDaysEnum.Monday) == WeekDaysEnum.Monday
&& (mask & WeekDaysEnum.Wednesday) == WeekDaysEnum.Wednesday
&& (mask & WeekDaysEnum.Friday) == WeekDaysEnum.Friday
&& (mask & WeekDaysEnum.Tuesday) != WeekDaysEnum.Tuesday)
{
howOften.SelectedIndex = 3;
}
else if ((mask & WeekDaysEnum.Tuesday) == WeekDaysEnum.Tuesday && (mask & WeekDaysEnum.Thursday) == WeekDaysEnum.Thursday)
{
howOften.SelectedIndex = 4;
}
else
{
howOften.SelectedIndex = 5;
}
break;
case RecurrenceTypeEnum.Monthly:
howOften.SelectedIndex = 6;
break;
case RecurrenceTypeEnum.Yearly:
howOften.SelectedIndex = 7;
break;
}
if (_appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
recPanel.Visibility = Windows.UI.Xaml.Visibility.Visible;
howOften.IsEnabled = true;
recEndDate.DateTime = pattern.PatternEndDate;
recEndDateSet.IsChecked = !pattern.NoEndDate;
}
else
{
// 発生
recPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
howOften.IsEnabled = false;
}
}
}
private void howOften_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_appointment == null)
{
return;
}
_updatingRecurrence = true;
if (howOften.SelectedIndex == 0)
{
_appointment.ClearRecurrencePattern();
}
else
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
switch (howOften.SelectedIndex)
{
case 1:
pattern.RecurrenceType = RecurrenceTypeEnum.Daily;
break;
case 2:
pattern.RecurrenceType = RecurrenceTypeEnum.Workdays;
break;
case 3:
pattern.RecurrenceType = RecurrenceTypeEnum.Weekly;
pattern.DayOfWeekMask = WeekDaysEnum.Monday | WeekDaysEnum.Wednesday | WeekDaysEnum.Friday;
break;
case 4:
pattern.RecurrenceType = RecurrenceTypeEnum.Weekly;
pattern.DayOfWeekMask = WeekDaysEnum.Tuesday | WeekDaysEnum.Thursday;
break;
case 5:
pattern.RecurrenceType = RecurrenceTypeEnum.Weekly;
pattern.DayOfWeekMask = (WeekDaysEnum)Enum.Parse(typeof(WeekDaysEnum), Enum.GetName(typeof(DayOfWeek), _appointment.Start.DayOfWeek), true);
break;
case 6:
pattern.RecurrenceType = RecurrenceTypeEnum.Monthly;
pattern.DayOfMonth = _appointment.Start.Day;
break;
case 7:
pattern.RecurrenceType = RecurrenceTypeEnum.Yearly;
pattern.DayOfMonth = _appointment.Start.Day;
pattern.MonthOfYear = _appointment.Start.Month;
break;
}
}
_updatingRecurrence = false;
UpdateRecurrenceState();
UpdateCalendars();
}
private void recEndDateSet_Unchecked(object sender, RoutedEventArgs e)
{
if (_appointment != null && _appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
pattern.NoEndDate = true;
}
}
private void recEndDateSet_Checked(object sender, RoutedEventArgs e)
{
if (_appointment != null && _appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
pattern.NoEndDate = false;
pattern.PatternEndDate = recEndDate.DateTime.Value;
}
}
private void recEndDate_DateTimeChanged(object sender, object e)
{
if (_appointment != null && _appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
pattern.PatternEndDate = recEndDate.DateTime.Value;
}
}
#endregion
//-----------------------------------------------------------------------------------
#region ** times
private void startCalendar_DateTimeChanged(object sender, object e)
{
if (_appointment != null)
{
_appointment.Start = startCalendar.DateTime.Value;
if (_appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
pattern.PatternStartDate = startCalendar.DateTime.Value;
pattern.StartTime = _appointment.Start;
}
UpdateCalendars();
}
}
private void chkAllDay_Checked(object sender, RoutedEventArgs e)
{
if (_appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
pattern.StartTime = _appointment.Start;
pattern.Duration = _appointment.Duration;
}
startCalendar.EditMode = endCalendar.EditMode = C1.Xaml.DateTimeEditors.C1DateTimePickerEditMode.Date;
UpdateWindowHeader();
UpdateCalendars();
}
private void chkAllDay_Unchecked(object sender, RoutedEventArgs e)
{
_appointment.Start = _appointment.Start.Add(_defaultStart);
_appointment.Duration = _defaultDuration;
if (_appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
pattern.StartTime = _appointment.Start;
pattern.Duration = _appointment.Duration;
}
startCalendar.EditMode = endCalendar.EditMode = C1.Xaml.DateTimeEditors.C1DateTimePickerEditMode.DateTime;
UpdateWindowHeader();
UpdateCalendars();
}
private void endCalendar_DateTimeChanged(object sender, object e)
{
if (_appointment != null)
{
DateTime end = endCalendar.DateTime.Value;
if (_appointment.AllDayEvent)
{
end = end.AddDays(1);
}
if (end < Appointment.Start)
{
endCalendar.BorderBrush = endCalendar.Foreground = new SolidColorBrush(Colors.Red);
endCalendar.BorderThickness = new Thickness(2);
ToolTipService.SetToolTip(endCalendar, C1.Silverlight.Schedule.Resources.C1_Schedule_Exceptions.StartEndValidationFailed);
saveButton.IsEnabled = false;
}
else
{
_appointment.End = end;
if (!saveButton.IsEnabled)
{
saveButton.IsEnabled = true;
endCalendar.ClearValue(Control.ForegroundProperty);
endCalendar.ClearValue(Control.BorderBrushProperty);
endCalendar.ClearValue(Control.BorderThicknessProperty);
endCalendar.ClearValue(ToolTipService.ToolTipProperty);
}
}
if (_appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
pattern.StartTime = _appointment.Start;
pattern.Duration = _appointment.Duration;
}
}
}
private void UpdateCalendars()
{
if (_appointment.RecurrenceState == RecurrenceStateEnum.Master)
{
RecurrencePattern pattern = _appointment.GetRecurrencePattern();
startCalendar.DateTime = pattern.StartTime;
DateTime end = pattern.EndTime;
if (_appointment.AllDayEvent)
{
end = end.AddDays(-1);
}
endCalendar.DateTime = end;
}
else
{
startCalendar.DateTime = _appointment.Start;
DateTime end = _appointment.End;
if (_appointment.AllDayEvent)
{
end = end.AddDays(-1);
}
endCalendar.DateTime = end;
}
if (!saveButton.IsEnabled)
{
saveButton.IsEnabled = true;
endCalendar.ClearValue(Control.BackgroundProperty);
endCalendar.ClearValue(Control.ForegroundProperty);
endCalendar.ClearValue(Control.BorderBrushProperty);
endCalendar.ClearValue(Control.BorderThicknessProperty);
endCalendar.ClearValue(ToolTipService.ToolTipProperty);
}
}
#endregion
//-----------------------------------------------------------------------------------
#region ** misc props
private void UpdateWindowHeader()
{
string result;
string subject = string.Empty;
bool allDay = false;
if (_appointment != null)
{
subject = _appointment.Subject;
allDay = chkAllDay.IsChecked.Value;
}
if (String.IsNullOrEmpty(subject))
{
subject = C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.Untitled;
}
if (allDay)
{
result = C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.Event + " - " + subject;
}
else
{
result = C1.Silverlight.Schedule.Resources.C1_Schedule_EditAppointment.Appointment + " - " + subject;
}
Header = result;
}
private void subject_TextChanged(object sender, TextChangedEventArgs e)
{
subject.GetBindingExpression(TextBox.TextProperty).UpdateSource();
UpdateWindowHeader();
}
#endregion
//-----------------------------------------------------------------------------------
#region ** navigation
private void saveButton_Click(object sender, RoutedEventArgs e)
{
_appointment.ReminderTimeBeforeStart = reminderTime.Value.Value;
subject.Focus(Windows.UI.Xaml.FocusState.Programmatic);
location.GetBindingExpression(TextBox.TextProperty).UpdateSource();
body.GetBindingExpression(TextBox.TextProperty).UpdateSource();
_parentWindow.DialogResult = MessageBoxResult.OK;
}
private void backButton_Click(object sender, RoutedEventArgs e)
{
_parentWindow.DialogResult = MessageBoxResult.Cancel;
}
#pragma warning disable 1591
protected override void OnKeyDown(Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Escape)
{
backButton_Click(null, null);
}
else if (e.Key == Windows.System.VirtualKey.S && KeyboardUtil.Ctrl)
{
saveButton_Click(null, null);
}
else if (e.Key == Windows.System.VirtualKey.D && KeyboardUtil.Ctrl)
{
_scheduler.DeleteAppointment(_appointment.Tag as Appointment);
}
base.OnKeyDown(e);
}
#pragma warning restore 1591
#endregion
}
}
|